package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;
import com.example.project.entity.UserOnline;


/**
 * 在线用户 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface UserOnlineMapper {

	/**
	 * 通过会话序号查询信息
	 * 
	 * @param sessionId 会话ID
	 * @return 在线用户信息
	 */
	@Select("select sessionId, login_name, dept_name, ipaddr, login_location, browser, os, status, start_timestamp, last_access_time, expire_time from t_user_online where sessionId = #{sessionId}")
	public UserOnline selectOnlineById(String sessionId);

	/**
	 * 通过会话序号删除信息
	 * 
	 * @param sessionId 会话ID
	 * @return 在线用户信息
	 */
	@Delete("delete from t_user_online where sessionId = #{sessionId}")
	public int deleteOnlineById(String sessionId);

	/**
	 * 保存会话信息
	 * 
	 * @param online 会话信息
	 * @return 结果
	 */
	@Insert("insert into t_user_online(sessionId, login_name, dept_name, ipaddr, login_location, browser, os, status, start_timestamp, last_access_time, expire_time) values (#{sessionId}, #{loginName}, #{deptName}, #{ipaddr}, #{loginLocation}, #{browser}, #{os}, #{status}, #{startTimestamp}, #{lastAccessTime}, #{expireTime}) on duplicate key update login_name=values(login_name), dept_name=values(dept_name), ipaddr=values(ipaddr), login_location=values(login_location), browser=values(browser), os=values(os), status=values(status), start_timestamp=values(start_timestamp), last_access_time=values(last_access_time), expire_time=values(expire_time)")
	public int saveOnline(UserOnline online);

	/**
	 * 查询会话集合
	 * 
	 * @param userOnline 会话参数
	 * @return 会话集合
	 */
	@Select("<script>" +
			"select sessionId, login_name, dept_name, ipaddr, login_location, browser, os, status, start_timestamp, last_access_time, expire_time from t_user_online" +
			"<where>" +
			"<if test='ipaddr != null and ipaddr != \"\"'> AND ipaddr like concat('%', #{ipaddr}, '%')</if>" +
			"<if test='loginName != null and loginName != \"\"'> AND login_name like concat('%', #{loginName}, '%')</if>" +
			"</where>" +
			"</script>")
	public List<UserOnline> selectUserOnlineList(UserOnline userOnline);

	/**
	 * 查询过期会话集合
	 * 
	 * @param lastAccessTime 过期时间
	 * @return 会话集合
	 */
	@Select("select sessionId, login_name, dept_name, ipaddr, login_location, browser, os, status, start_timestamp, last_access_time, expire_time from t_user_online o WHERE o.last_access_time <= #{lastAccessTime} ORDER BY o.last_access_time ASC")
	public List<UserOnline> selectOnlineByExpired(String lastAccessTime);
}
